package cn.tedu.egarticle0618.controller;

import cn.tedu.egarticle0618.base.response.JsonResult;
import cn.tedu.egarticle0618.mapper.UserMapper;
import cn.tedu.egarticle0618.pojo.dto.UserArticleParam;
import cn.tedu.egarticle0618.pojo.dto.UserLoginParam;
import cn.tedu.egarticle0618.pojo.entity.Article;
import cn.tedu.egarticle0618.pojo.vo.ArticleVO;
import cn.tedu.egarticle0618.pojo.vo.UserVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpSession;
import org.hibernate.validator.constraints.Range;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

@Tag(name = "01-用户模块")
@Slf4j
@Validated
@RestController
@RequestMapping("v1/user/")
public class UserController {
    @Autowired
    private UserMapper userMapper;

    @PostMapping("login")
    @Operation(summary = "用户登录")
    @ApiOperationSupport(order = 10)
    public JsonResult login (@Validated @RequestBody UserLoginParam  userLoginParam, HttpSession session) {
        log.debug("userLoginParam = " + userLoginParam);
        UserVO userVO = userMapper.selectByUsernameAndPassword(userLoginParam.getUsername());
        log.debug("userVO = " + userVO);
        session.getAttribute("user");
        return JsonResult.ok(userVO);
    }

    @PostMapping("article")
    @Operation(summary = "用户发布文章")
    @ApiOperationSupport(order = 20)
    public JsonResult addArticle(@Validated @RequestBody UserArticleParam userArticleParam, HttpSession session){
        log.debug("UserArticleParam = "+ userArticleParam);
        UserVO userVO = (UserVO) session.getAttribute("user");
        log.debug("userVO =" + userVO);
        Article article = new Article();
        BeanUtils.copyProperties(userArticleParam,article);
        article.setUserId(userVO.getId());
        userMapper.insertArticle(article);
        return JsonResult.ok();
    }

    @GetMapping("selectArticleId")
    @Operation(summary = "查询指定文章功能")
    @ApiOperationSupport(order=30)
    public JsonResult selectArticleId(@Range(min = 1,max = 100,message = "文章ID必须在1-100之间") Long id){
        log.debug("id="+ id);
        ArticleVO articleVO = userMapper.selectByArticleId(id);
        return JsonResult.ok(articleVO);
    }

}
